package com.eazyshop.goods.controller;


import com.eazyshop.common.pojo.Result;
import com.eazyshop.common.pojo.StatusCode;
import com.eazyshop.goods.pojo.Brand;
import com.eazyshop.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
// 开启跨域支持
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<List<Brand>> findList(){

        List<Brand> list = brandService.findList();
        Result<List<Brand>> result = new Result<>(true, StatusCode.OK, "查询成功", list);

        return result;

    }
    @GetMapping("/{id}")
    public   Result<Brand> findById(@PathVariable("id") Integer id){
        Brand byId = brandService.findById(id);

        return  new Result(true,StatusCode.OK,"查询成功",byId);

    }
    /**
     * 新添加商品
     */
    @PostMapping
    public Result  addBrand(@RequestBody Brand brand){
        brandService.addBrand(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 编辑商品
     * @param brand
     * @return
     */
    @PutMapping
    public Result updateBrand(@RequestBody Brand brand){

        brandService.updateBrand(brand);

        return new Result(true,StatusCode.OK,"编辑成功");
    }

    /**
     * 依据id删除商品
     */

    @DeleteMapping("/{id}")
    public Result deleteBrandById(@PathVariable Integer id){

        brandService.deleteBrandById(id);
        return new Result(true,StatusCode.OK,"删除成功");

    }
    /**
     * 多条件查询
     */
    @GetMapping("/search")
    public Result findListByConditions(@RequestParam Map<String, Object> searchMap){

        List<Brand> list = brandService.findListByConditions(searchMap);

        Result<List> result = new Result<>(true, StatusCode.OK, "查询成功", list);

        return result;
    }
    /**
     * 品牌分页查询
     */
    @GetMapping("/page/{pageNum}/{pageSize}")
    public Result  findByPage(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize")int pageSize){



        PageInfo<Brand> byPage = brandService.findByPage(pageNum, pageSize);


        Result<Object> result = new Result<>(true, StatusCode.OK, "分页查询成功", byPage);

        return  result;
    }
    /**
     * 分页查询+多条件查询
     */

    @GetMapping("/search/{pageNum}/{pageSize}")
    public Result findByPageAndConditions(@PathVariable("pageNum") int start,@PathVariable("pageSize")  int size, @RequestParam Map<String, Object> conditions){

        PageInfo<Brand> pageInfo = brandService.findByPageAndConditions(start, size, conditions);
        // 异常统一处理
        int j=1/0;
        Result result = new Result(true, StatusCode.OK, "查询成功", pageInfo);
        return  result;
    }

    /**
     *根据分类名称查询商品
     */
    @GetMapping("/category/{categoryName}")
    public Result findBrandListByCategoryName(@PathVariable("categoryName") String categoryName){

        List<Map> mapList = brandService.findBrandListByCategoryName(categoryName);

        return new Result(true,StatusCode.OK,"查询成功",mapList);
    }

}
